package manager;
import Database.Connectmysql;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

public class ManagerHome extends JFrame {
    private Object[][] books;
    private JTextField searchTextField;
    private JButton searchButton;
    private JTable table;
    private JMenuBar menubar;
    private JMenu setUp;
    private JMenu bookManage;
    private JMenu search;
    private JMenuItem resetPassword ;
    private JMenuItem addManager ;
    private JMenuItem  addNewBook ;
    private JMenuItem searchBook ;
    private JMenuItem searchUser ;
    private JMenuItem deleteBook ;
    private JMenuItem changeBookInformation ;
    private JScrollPane js;
    private DefaultTableModel modle;
    public ManagerHome( String MName){
        setTitle("管理员");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 1000, 800);
        setLayout(null);
        resetPassword = new JMenuItem("修改密码");
        addManager = new JMenuItem("添加管理员");
        addNewBook = new JMenuItem("添加图书");
        searchBook =new JMenuItem("借还记录");
        searchUser =new JMenuItem("查询用户");
        deleteBook = new JMenuItem("删除图书");
        changeBookInformation = new JMenuItem("修改图书信息");
        menubar = new JMenuBar();
        setUp = new JMenu("设置");
        bookManage = new JMenu("图书管理");
        search = new JMenu("查询");
        searchTextField = new JTextField();
        searchButton = new JButton("搜索");
        searchTextField.setBounds(100,10,200,30);
        searchButton.setBounds(350,10,70,30);
        setJMenuBar(menubar);
        menubar.add(bookManage);
        menubar.add(search);
        menubar.add(setUp);
        setUp.add(resetPassword);
        setUp.add(addManager);
        bookManage.add(addNewBook);
        bookManage.add(deleteBook);
        bookManage.add(changeBookInformation);
        search.add(searchBook);
        //add(js);
        add(searchButton);
        add(searchTextField);


        searchBook.addActionListener(e->{
            modle = new DefaultTableModel(new String[][]{},new String[]{"ID","借书人","书籍名","借书时间","借书情况"});
            modle.setRowCount(0);
            Connectmysql con = new Connectmysql(
                    "jdbc:mysql://localhost:3306/bookmanagement",
                    "root",
                    "root",
                    "com.mysql.cj.jdbc.Driver"
            );
            try {
                ResultSet rst = null;
                if (con.open()) {
                    rst = con.executeSqlSearch("select * from borrowrecord", new Object[]{});
                }
                int i=0;

                while (rst.next()){
                    modle.addRow(new String[]{rst.getString(1),
                            rst.getString(2),
                            rst.getString(3),
                            rst.getString(4),
                            rst.getString(5),

                    });
                }
                rst.close();
            }catch (ClassNotFoundException f) {
                f.printStackTrace();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            table = new JTable(modle);
            js = new JScrollPane(table);
            js.setBounds(0,100,1000,750);
            add(js);
            js.setVisible(true);
            setVisible(true);

        });

        

        searchButton.addActionListener(e->{
            if(searchTextField.getText()!=null){
                modle = new DefaultTableModel(new String[][]{},new String[]{"书籍id","书籍名","作者","入库时间"});
                modle.setRowCount(0);
                //books =new String[100][4];
                String searchText = searchTextField.getText();
                Connectmysql con = new Connectmysql(
                        "jdbc:mysql://localhost:3306/bookmanagement",
                        "root",
                        "root",
                        "com.mysql.cj.jdbc.Driver"
                );
                try {
                    ResultSet rst = null;
                    if (con.open()) {
                        rst = con.executeSqlSearch("select * from book where bookname like ?", new Object[]{searchText});
                    }
                    int i=0;

                    while (rst.next()){
                        modle.addRow(new String[]{rst.getString(1),
                                rst.getString(2),
                                rst.getString(3),
                                rst.getString(4),

                        });
                    }
                    rst.close();
                }catch (ClassNotFoundException f) {
                    f.printStackTrace();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
                table = new JTable(modle);
                js = new JScrollPane(table);
                js.setBounds(0,100,1000,750);
                add(js);
                js.setVisible(true);
                setVisible(true);

            }
            else{

            }
        });

        resetPassword.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new ResetPassword(MName);
            }
        });


        addManager.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new AddManager();
            }
        });
        addNewBook.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new AddBook();

            }
        });
        changeBookInformation.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new UpdateBook();
            }
        });
        deleteBook.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new DeleteBook();
            }
        });




        setVisible(true);
    }

}
